# File	  : EDT.pm
# Author  : Huzy
# Created : 2015/9/9 11:38:05
# Version : 1.01
#
#     Copyright (c) 2006 . All rights reserved.
#     This program is free software; you can redistribute it and/or
#     modify it under the same terms as Perl itself.
#/usr/opt/perl5/lib/site_perl/5.8.2/Bass/BASS_EXPORT_FILE.pm
package EDT;

use strict ;
use warnings ;
use FindBin;
use File::Spec;
use File::Basename;
use DBI ;
use Net::FTP;
use File::Copy;
use File::Path;
use Time::Local;
require 5.8.0 ;
require Exporter ;
use Net::SFTP::Foreign;
use Fcntl ':mode';

our ($VERSION, @ISA, @EXPORT ,@EXPORT_OK);
$VERSION = "1.01" ;

@ISA = qw(Exporter);

@EXPORT = qw( FTP_get_info
              FTP_put_file
              FTP_download_file
              FTP_get_file
              FTP_del_file
              FTP_get_list
              FTP_get_long_list
              FTP_rename_file
              FTP_Up_File
              file_compress
              file_decompress
              deltmp_file
			  iceCode
              run_fastexport_command
              methd_calate
              gen_verf_file
              showTime
              connectDB
              disconnectDB
              Ftp_Connect
			  Ftp_Disconnect
              Move_ControlFile
              curdate
              getDayTime
              );
#@EXPORT_OK = qw( $   );

my $Max_File_Size = $ENV{"AUTO_BASS1_MAX_FILE"};
if ( !defined($Max_File_Size) ) {
    $Max_File_Size = 2*1024*1024*1024;
}
my $bass1_ftp_basedir = "";       #"/data1/asiainfo/interface";

my $LOGDB = $ENV{"AUTO_LOGDB"};
if ( !defined($LOGDB) ) {
   $LOGDB = "PLOG2";
}
my $LOGON_STR = "";

my $AUTO_HOME=$ENV{"AUTO_HOME"};
if ( !defined($AUTO_HOME) )
{
	my $path = File::Spec->catfile($FindBin::Bin, $FindBin::Script);
    $AUTO_HOME = dirname(dirname(dirname( $path )));
}

my $AUTO_EDT_HOME=$ENV{"AUTO_EDT_HOME"};
if ( !defined($AUTO_EDT_HOME) )
{
    $AUTO_EDT_HOME = "$AUTO_HOME/EDT";
}

my $AUTO_EDT_TOOLS=$ENV{"AUTO_EDT_TOOLS"};
if ( !defined($AUTO_EDT_TOOLS) )
{
    $AUTO_EDT_TOOLS = "$AUTO_EDT_HOME/tools";
}

my $SECOND_WAIT = $ENV{"SECOND_WAIT"};
if ( !defined($SECOND_WAIT) ) {
   $SECOND_WAIT = 1800;
}

my $OS   = $^O;
print "This OS is:$OS\n";

sub deltmp_file
{
	  my($verf_file,$tmppath) = @_;
	  my $filename;
	  my ($prefix_file) = split(/.CHK/,$verf_file);

	  unless ( opendir(SRC_DIR, "$tmppath") )
    {
     	  showTime();print "ERROR - Unable to open dir  $tmppath $!.\n";
	   		return -1;
    }

    my @SRC_FILE=readdir(SRC_DIR);
    @SRC_FILE = sort @SRC_FILE;

    foreach $filename(@SRC_FILE)
    {
       if ($filename =~m"${prefix_file}\d{6}.AVL")
       {
       	 showTime();print "Found tmp file :$filename ,start to delete ...\n";
       	 unlink("$tmppath/$filename")||warn "delete file $filename failed!:$! \n" ;
       	}
     }
    return 0;
}

sub getDayTime
{
	my ($daytimemod,$assumedate,$offset,$offset_m) = @_;
	#print "OFFSET_M:$offset_m\n";
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
	my $daysofyear=0;
	#print "init:$year-$mon-$mday\n";
	unless(@_ == 4 or @_ == 3 or @_ == 2 or @_==1)
	{
		showTime();print "parameter is not right\n";
		return 0;
	}
	#日期偏移量
	if ( !defined($offset) ) {
		$offset = 0;
	}
	#月偏移量
	if ( !defined($offset_m) ) {
		$offset_m = 0;
	}
	#$offset_m--;
	#print "offset:$offset\n";
	#制定日期
	if ( !defined($assumedate)||$assumedate eq "" ||$assumedate eq "now") {
		$assumedate="now";
		$year+=1900;
	}
	else
	{
		$year=substr($assumedate,0,4);
		$mon=substr($assumedate,4,2);
		$mday=substr($assumedate,6,2);
		#$year=$year-1900;
		$mon--;
		if ($mon==-1) {$mon=11;}
		if ($mday<1 || $mday>31)
		{
			print "The assumedate is invaild.\n";
			return -1;
		}
	}
	#print "offset_m:$offset_m\n";
	$mon++;
	$mon=$mon-($offset_m);
	if ($mon<1 || $mon>12){
		#print "MON:$mon\n";
		if($mon <1)
		{
			$year-=int(abs($mon)/12)+1;
			#if ($mon % 12==0) 
			#{
			#	$year-=int(abs($mon)/12)+1;
			#}
			#elsif (abs($mon) < 12)
			#{
			#	$year-=int(abs($mon)/12)+1;
			#}
			#else
			#{
			#	$year-=int(abs($mon)/12)+1;
			#}
		}
		elsif ($mon>12)
		{
			if ($mon % 12==0) 
			{
				$year+=int(abs($mon)/12)-1;
			}
			else
			{
				$year+=int(abs($mon)/12);
			}
		}
		
		#if (abs($mon/12)==1){
		if ($mon % 12 == 0 ){
			#print "整除12:$mon\n";
			$mon=12;
		}
		else
		{
			#print "取余:$mon\n";
			$mon=($mon+12) % 12;
		}
	}
	$mon--;
	#print "mon2:$mon\n";

	if ($mon == 1)
	{
		if ( (($year % 4==0) && ($year % 100 != 0)) || (($year % 100==0) && ($year % 400 == 0)) )
		{
			if ($mday>29)
			{
				$mday=29;
			}
		}
		elsif ($mday>28)
		{
			$mday=28;
		}
	}
	#print "YMD:$year-$mon-$mday\n";

	if ((grep(/^$mon$/,(0,2,4,6,7,9,11))) && $mday>=31)
	{
		#print "LEAP1:$year-$mon-$mday\n";
		$mday=31;
	}
  elsif ((grep(/^$mon$/,(3,5,8,10))) && $mday>30)
	{
		#print "LEAP2:$year-$mon-$mday\n";
		$mday=30;
	}

	#print "mon:$mon\n";

	if ( (($year % 4==0) && ($year % 100 != 0)) || (($year % 100==0) && ($year % 400 == 0)) )
	{
		$daysofyear=366;
	}
	else
	{
		$daysofyear=365;
	}

	my $seconds = timelocal($sec,$min,$hour,$mday,$mon,$year-1900);
	$seconds-=86400*$offset;
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($seconds);


	$year += 1900;
	$mon = sprintf("%02d", $mon + 1);
	$mday = sprintf("%02d", $mday);
	my $today = "${year}${mon}${mday}";
	my $ftoday = "${year}-${mon}-${mday}";

	$hour = sprintf("%02d", $hour);
	$min  = sprintf("%02d", $min);
	$sec  = sprintf("%02d", $sec);
	my $now = "${hour}:${min}:${sec}";
	my $strnow="${hour}${min}${sec}";
	if($daytimemod eq "time"){
		return $now;
	}
	elsif($daytimemod eq "today"){
		return $today;
	}
	elsif ($daytimemod eq "strdaytime") {
		return "$today$strnow";
	}
	elsif ($daytimemod eq "daytime") {
		return "$ftoday $now";
	}
		if($daytimemod eq "year"){
		return $year;
	}
	elsif($daytimemod eq "mon"){
		return $mon;
	}
	elsif ($daytimemod eq "day") {
		return $mday;
	}
	elsif($daytimemod eq "yearmon"){
		return "$year$mon";
	}
	elsif($daytimemod eq "daysofyear"){
		return "$daysofyear";
	}
	elsif($daytimemod eq "daysofweek"){
		$year -= 1900;
    $mon -= 1;
    my $timenum = Time::Local::timelocal(0, 0, 0, $mday, $mon, $year);
    my @timestr = localtime($timenum);
    my $daysofweek  = $timestr[6];
		return "$daysofweek";
	}
	elsif ($daytimemod eq "yesterday") {
    $year -= 1900;
    $mon -= 1;
    my $timenum = Time::Local::timelocal(0, 0, 0, $mday, $mon, $year);
    $timenum -= 86400;  # plus one day second number
    my @timestr = localtime($timenum);
    $year  = $timestr[5] + 1900;
    $mon = sprintf("%02d", $timestr[4] + 1);
    $mday  = sprintf("%02d", $timestr[3]);
    return "$year$mon$mday";
	}
		elsif ($daytimemod eq "lastdayYear") {
    $year -= 1900;
    $mon -= 1;
    my $timenum = Time::Local::timelocal(0, 0, 0, $mday, $mon, $year);
    $timenum -= 86400;  # plus one day second number
    my @timestr = localtime($timenum);
    $year  = $timestr[5] + 1900;
    $mon = sprintf("%02d", $timestr[4] + 1);
    $mday  = sprintf("%02d", $timestr[3]);
    return "$year";
	}
		elsif ($daytimemod eq "lastdayMon") {
    $year -= 1900;
    $mon -= 1;
    my $timenum = Time::Local::timelocal(0, 0, 0, $mday, $mon, $year);
    $timenum -= 86400;  # plus one day second number
    my @timestr = localtime($timenum);
    $year  = $timestr[5] + 1900;
    $mon = sprintf("%02d", $timestr[4] + 1);
    $mday  = sprintf("%02d", $timestr[3]);
    return "$mon";
	}
		elsif ($daytimemod eq "lastdayDay") {
    $year -= 1900;
    $mon -= 1;
    my $timenum = Time::Local::timelocal(0, 0, 0, $mday, $mon, $year);
    $timenum -= 86400;  # plus one day second number
    my @timestr = localtime($timenum);
    $year  = $timestr[5] + 1900;
    $mon = sprintf("%02d", $timestr[4] + 1);
    $mday  = sprintf("%02d", $timestr[3]);
    return "$mday";
	}
	elsif ($daytimemod eq "quarter") {
		$mon -= 1;
		#print "MON2:$mon\n";
		my $quarter=0;
		my @row1=qw{0 1 2};    #1 2 3 月
		my @row2=qw{3 4 5};    #4 5 6 月
		my @row3=qw{6 7 8};    #7 8 9 月
		my @row4=qw	{9 10 11}; #10 11 12 月
	
		if (grep m/^$mon$/, @row1)
		{
			$quarter=1;
		}
		elsif (grep m/^$mon$/, @row2)
		{
			$quarter=2;
		}
		elsif (grep m/^$mon$/, @row3)
		{
			$quarter=3;
		}
		elsif (grep m/^$mon$/, @row4)
		{
			$quarter=4;
		}
		else
		{
			print "XX";
		}
		return $quarter;
	}
	else {
		return "";
	}
}

sub gen_verf_file
{
	my($dir,$verf_file,$decompressflag,$datafiledir) = @_;
	#i_12100_CM_01001_20150830_000.verf
	
	my $dir_prefix="";
	my $dir_txdate="";
	if ($verf_file=~m/(\S+)(\d{14})(\S*)\.(\S*)/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$1;
		$dir_txdate=substr($2,0,8);
	}
	elsif ($verf_file=~m/(\S+)(\d{8})(\S*)\.(\S*)/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$1;
		$dir_txdate=$2;
	}
	elsif ($verf_file=~m/(\S+)(\d{6})(\S*)\.(\S*)/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$1;
		$dir_txdate=$2."01";
	}
	elsif ($verf_file=~m/(\S+)(\d{4})\-(\d{2})\-(\d{2})(\S*)\.(\S*)/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$2."\n";
		#print "3:".$3."\n";
		#print "4:".$4."\n";
		$dir_prefix=$1;
		$dir_txdate=$2.$3.$4;
	}
	else
	{
		$dir_prefix=$verf_file;
		$dir_prefix=~s/dir\.//g;
		$dir_txdate="";
	}
	my $dirFileName="dir.$dir_prefix$dir_txdate";
	# 防止原有控制文件与新建控制文件同名
	if ($dirFileName eq $verf_file) {
		move("$dir/$verf_file", "$dir/$verf_file".".old");
		$verf_file = $verf_file.".old";
	}

	unless(open(OUTFILE, ">$dir/$dirFileName"))
	{
		showTime();print STDOUT "Can not create file $dir/$dirFileName\n";
		return -1;
	}
	unless(open(INFILE, "$dir/$verf_file"))
	{
		showTime();print STDOUT "gen_verf_file Can not open file $dir/$verf_file\n";
		return -1;
	}

	my @sourceVerfCons = <INFILE>;
	close(INFILE);
	foreach my $sourceVerfCon(@sourceVerfCons)
	{
		my ($filename,$filesize)=split(/\s+/,$sourceVerfCon);
		if ($decompressflag eq "Y")
		{
			$filename=~s/\.gz$//g;
			$filesize =  (stat("$datafiledir/$filename"))[7];
		}
		print OUTFILE "$filename $filesize\n";
	}
	close(OUTFILE);
	if (-e "$dir/$verf_file")
	{
		unlink("$dir/$verf_file");
	}
	return $dirFileName;
}


sub gen_dir_file
{
	my($tgttempdir,$dataFileFullName) = @_;
	#print "$dataFileFullName\n";
	my @words=split(/\//,$dataFileFullName);
	my $dataFileName=$words[$#words];
	my $dataDir="";
	for(my $i=0;$i<=$#words-1;$i++)
	{
		$dataDir=$dataDir."/".$words[$i];
	}
	$dataDir=~s/\/\//\//gi;
	#print "$dataFileName\n";
	my $prefix_file = (split(/\./,$dataFileName))[0];
	my $suffix_file = (split(/\./,$dataFileName))[1];
	#print "$prefix_file\n";
	my $dir_prefix="";
	my $dir_txdate="";
	if ($prefix_file=~m/(\S+)(\d{14})/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$1;
		$dir_txdate=substr($2,0,8);
	}
	elsif ($prefix_file=~m/(\S+)(\d{8})/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$1;
		$dir_txdate=$2;
	}
	elsif ($prefix_file=~m/(\S+)(\d{6})/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$1;
		$dir_txdate=$2."01";
	}
	elsif ($prefix_file=~m/(\S+)(\d{4})\-(\d{2})\-(\d{2})/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$2."\n";
		#print "3:".$3."\n";
		#print "4:".$4."\n";
		
		$dir_prefix=$1;
		$dir_txdate=$2.$3.$4;
	}
	elsif ($prefix_file=~m/(\S+)_(\d{2})_(\d{2})_(\d{4})/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$1;
		$dir_txdate=$4.$2.$3;
	}
	elsif ($prefix_file=~m/^operation/ && $suffix_file=~m/(\w{3})_(\d{4})-(\d{2})-(\d{2})/)
	{
		#print "1:$prefix_file\n";
		#print "2:".$1."\n";
		#print "3:".$2."\n";
		$dir_prefix=$prefix_file;
		$dir_txdate=$2.$3.$4;
	}
	else
	{
		$dir_prefix=substr($prefix_file,0,20);
		$dir_txdate=getDayTime("today");
	}
	my $dirFileName="dir.$dir_prefix$dir_txdate";
	
	showTime();print "EDT make dirFile name is:$dirFileName\n";
	
	unless(open(OUTFILE, ">$tgttempdir/$dirFileName"))
	{
		showTime();print STDOUT "gen_dir_file Can not open file $tgttempdir/$dirFileName\n";
		return -1;
	}
	
	my $filesize =  (stat("$dataFileFullName"))[7];
	my $filerow=0;
	open (FH,"$dataFileFullName") or die "get_dir_file Can't open the file:$dataFileFullName\n";showTime();
	++$filerow while (<FH>);
	close FH;
	
	my @dataFileInfo =  stat("$dataFileFullName");

	my @t = localtime $dataFileInfo[9];

	
	#my @t = localtime (stat("$dataFileFullName"))[9];
	
	my $file_createtime =sprintf("%04u%02u%02u%02u%02u%02u", $t[5] +1900,$t[4] + 1, $t[3], $t[2],$t[1],$t[0]) ;
	
	printf OUTFILE "$dataFileName $filesize $filerow $dir_txdate $file_createtime\n";
	close(OUTFILE);
	
	move("$tgttempdir/$dirFileName","$dataDir/$dirFileName");
	return 0;
}


sub run_fastexport_command
{
	my($fxp_table,$fxp_date,$fxp_file,$fxp_sql,$outmod_flag) = @_;
	my $exportstr = "";
	my $mod_file = "";

	$outmod_flag = 0 unless($outmod_flag);
	my $OS = $^O;
	$OS =~ tr [A-Z][a-z];
	if($outmod_flag == 1)
	{
		if ( $OS eq "mswin32" )
    {
        $mod_file = "mod_out.dll";
    }
    elsif ( $OS eq "aix")
    {
        $mod_file = "$AUTO_EDT_HOME/lib/outmod/nm_mod_out.so";
    }
		$exportstr = ".EXPORT OUTMOD $mod_file \n   MODE RECORD ;";
	}
	elsif($outmod_flag == 2)
	{
		if ( $OS eq "mswin32" )
    {
        $mod_file = "varfile.dll";
    }
    elsif ( $OS eq "aix")
    {
        $mod_file = "$AUTO_EDT_HOME/lib/outmod/VARFILE.DLL";
    }
		$exportstr = ".EXPORT OUTMOD $mod_file \n   MODE RECORD ;";
	}
	else
	{
	  $exportstr = ".EXPORT OUTFILE $fxp_file\n  MODE RECORD FORMAT TEXT;" ;
	  #$fxp_sql =~ s/SELECT/SELECT \nCAST((csum(1,0) (format 'z(8)')) as char(8)),/gi;
	}

	my $LOGtabString=substr("${fxp_table}$fxp_date",0,30);

	chomp($LOGtabString);

	my $rc = open(FEXP, "| fexp");

	unless ($rc) {
		print "Could not invoke FastExport command\n";
		return -1;
	}

	# ------ Below are FastExport scripts ------
print FEXP <<ENDOFINPUT;
.LOGTABLE $LOGDB.$LOGtabString;
${LOGON_STR}

.BEGIN EXPORT SESSIONS 8;
$exportstr
$fxp_sql
.END EXPORT;
ENDOFINPUT

	 close(FEXP);
   my $RET_CODE = $? >> 8;
   return $RET_CODE;
}

sub file_compress
{
	my($filename)=@_;
	print "文件压缩输入名称：$filename...\n";
	my @arrname;
	my $RET_CODE=0;
  $RET_CODE=`gzip -9 $filename`;
  if($RET_CODE)
  {
    showTime();print "File compress error!\n";
    return -1;
  }
  my @arrtemp=split(/\//,$filename);
  chomp (@arrtemp);
  my $i=$#arrtemp;
  print "文件名称: $arrtemp[$i]\n";
  my $comprs_file=$arrtemp[$i].".gz";
  push @arrname,$comprs_file;
  return @arrname;
}

sub file_decompress
{
	my($dir,$filename)=@_;
	#my @arrname;
	
  print "文件解压缩前名称: $filename\n";
  
  my $decopresfilename=$filename;
  $decopresfilename=~s/\.gz$//g;
  print "文件解压缩后名称: $decopresfilename\n";
  if (-e "$dir/$decopresfilename")
  {
  	unlink("$dir/$decopresfilename");
  }
  my $filefullname="$dir/$filename";
	print "gzip -d $filefullname\n";
	my $RET_CODE=0;
  $RET_CODE=`gzip -d $filefullname`;
  print "RET_CODE:$RET_CODE,$?\n";
  if($RET_CODE)
  {
    showTime();print "File compress error!\n";
    return -1;
  }
  
  #push @arrname,$comprs_file;
  return $decopresfilename;
}

sub showTime
{
   	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
   	my $current = "";

   	$hour = sprintf("%02d", $hour);
   	$min  = sprintf("%02d", $min);
   	$sec  = sprintf("%02d", $sec);

   	$current = "${hour}:${min}:${sec}";
	  print "[$current] ";
   	return $current;
}

sub yesdate
{
	  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time()-86400);
    $year +=1900 ;
    $mon =sprintf("%02d",$mon+1) ;
    $mday=sprintf("%02d",$mday ) ;
    return ${year}.${mon}.${mday};
}

sub curdate
{
	  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time());
    $year +=1900 ;
    $mon =sprintf("%02d",$mon+1) ;
    $mday=sprintf("%02d",$mday ) ;
    return ${year}.${mon}.${mday};
}

sub curtime
{
   	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
   	my $current = "";

   	$hour = sprintf("%02d", $hour);
   	$min  = sprintf("%02d", $min);
   	$sec  = sprintf("%02d", $sec);

   	$current = "${hour}${min}${sec}";
   	return $current;
}

sub iceCode
{
    my ($LOGON_STR) = @_;
    $LOGON_STR = `${AUTO_EDT_TOOLS}/IceCode.exe "$LOGON_STR"`;
	my ($logoncmd, $userpw) = split(' ',$LOGON_STR);

    chop($userpw);
    my ($USER, $PASSWD) = split(',' , $userpw);
    return ($USER, $PASSWD);
}

sub connectDB
{
	  my ($logon_file,$dsource)= @_ ;

	  my $connectString;
    my $OS   = $^O;
    $OS =~ tr [A-Z][a-z];
    if ( $OS eq "svr4" || $OS eq "aix" )
    {
      	$connectString = "dbi:Teradata:${dsource}";
    }
    elsif ( $OS eq "mswin32" )
    {
      	$connectString = "dbi:ODBC:${dsource}";
    }
    #elsif ( $OS eq "aix" )
    #{
    #  	$connectString = "dbi:ODBC:${dsource}";
    #}

    unless(open(LOGONFILE_H, "$logon_file"))
    {
    	 showTime();print "Can't open $logon_file file:$!\n";
    	 return undef;
    	}

    select((select(LOGONFILE_H),$|=1)[0]);
    my $LOGON_STR = <LOGONFILE_H>;
    close(LOGONFILE_H);
    my ($USER, $PASSWD) = iceCode($LOGON_STR);
	my $dbh= DBI->connect($connectString, $USER, $PASSWD,{ RaiseError => 0, AutoCommit => 1 ,PrintError => 0});
    return $dbh;
}


sub disconnectDB
{
   my ($dbh) = @_;

   unless ( $dbh->disconnect() ) { return 1; }

   return 0;
}
sub Move_ControlFile
{
	my ($src_dir,$src_file,$des_dir,$des_file,$deal_date)=@_;

	if ( !-d "$des_dir/$deal_date")
	{
		mkpath("$des_dir/$deal_date",0,0755);
	}

	if(!open(FH,"$src_dir/$src_file") )
	{
	  print "Can't open file $src_dir/$des_file:$!\n";
	  return 1;
	}
	my @info = <FH>;
	close(FH);

	my $i;
	my $pass_rc=0;
	foreach $i ( @info)
	{
	   #my $filename=substr($i,0,40);
	   my $filename=(split(' ',$i))[0];
     $filename=~s/\s+//;
	   my $pass_ret = rename("$src_dir/$filename","$des_dir/$deal_date/$filename");
	   if(!defined($pass_ret))
	   {
	    	print "File $filename move failed:$!\n";
	    	$pass_rc=$pass_rc+1;
	   }
	}
	if (  $pass_rc == 0 )
	{
		my $pass_ret = rename("$src_dir/$src_file","$des_dir/$deal_date/$src_file");
		if(defined($pass_ret))
		{
			#print "File $des_file pass successful\n";
			$pass_rc=0;
		}
		else
		{
			print "File $des_file pass failed:$!\n";
			$pass_rc=1;
		}
	}
	return $pass_rc;
}

sub Ftp_Connect
{
	my($protocol,$ftpserver,$port,$logon_ftp,$Is_Pasv,$try_limit) = @_;
	
	my ($USER, $PASSWD)=iceCode($logon_ftp);
	unless(@_ == 6 or @_ == 5 or @_ == 4 or @_ == 3)
	{
		print "The paras are not right\n";
		return undef;
	}

	$try_limit = 5 unless($try_limit);

	my $ftp = ();
	my $i = 0;
  	if ($Is_Pasv eq "Y")
  	{
		$Is_Pasv=1;
  	}
  	else
	{
		$Is_Pasv=0;
	}

	if ($protocol eq "FTP") {
		while(!defined($ftp))
		{
			print "connecting ftpserver...\n";
			$ftp=Net::FTP->new($ftpserver,Debug => 0, Timeout => 600,Port=>$port, Passive => $Is_Pasv) ;
			#print "FTP->NEW:$ftp...\n";
			if(!defined($ftp))
			{
				$i++;
				#email()if($i==$try_limit);
				print "Try again $i times.\n";
				if($i==$try_limit)
				{
					showTime();print "Unable to connect ftp Server[$ftpserver], Reach FTP Connect Max Try Limit...\n";
					return undef;
				}

				sleep 3;
			}
		}

		my $rc = 0;
		$i = 0;
		while(!$rc)
		{
			#showTime();print "Connect to ftp Server Current User : [$USER]\n";
			$rc=$ftp->login("$USER","$PASSWD");
			print "login value:$rc\n";
			if(!$rc)
			{
				showTime();print "Unable Connect to ftp Server[$ftpserver] :",$ftp->message;
				print "\n";
				$i++;
				if($i==$try_limit)
				{
					showTime();print "Unable to connect ftp Server[$ftpserver], Reach FTP Connect Max Try Limit...\n";
					return undef;
				}
				sleep 3;
			}
			else
			{
				$ftp->binary();
			}
		}
	} elsif ($protocol eq "SFTP") {
		while(!defined($ftp))
		{
			showTime();print "connecting sftpserver...\n";
            my %args = (user => $USER, password => $PASSWD, port => $port, more => [-o => 'StrictHostKeyChecking no'],);
			$ftp = Net::SFTP::Foreign->new($ftpserver, %args);
			if(!defined($ftp))
			{
				$i++;
				print "Try again $i times.\n";
				if($i==$try_limit)
				{
					showTime();print "Unable to connect ftp Server[$ftpserver], Reach FTP Connect Max Try Limit...\n";
					return undef;
				}
				sleep 3;
			}
		}
	}
	
   	return $ftp;
}

sub Ftp_Disconnect
{
	my($protocol,$ftp) = @_;
	if ($protocol eq "FTP") {
		$ftp->quit();
	} elsif ($protocol eq "SFTP") {
		$ftp->disconnect();
	}
}

sub FTP_Up_File
{
	my($protocol,$ftp,$des_dir,$des_file,$remote_dir) = @_;
	print "上传文件： $des_file\n";
	unless(@_ == 5)
	{
		showTime();print "parameter is not right\n";
		return 1;
	}

	unless($remote_dir =~ m'^/.*')
	{
		$remote_dir = "$remote_dir";
	}

	if(!open(FH,"$des_dir/$des_file"))
	{
		print "Can't open file $des_dir/$des_file:$!\n";
		return 1;
    }

	my @info = <FH>;
	close(FH);

    my $rc;
    my $ftpmsg;
    if ($protocol eq "FTP") {
	    $rc = $ftp->cwd("$bass1_ftp_basedir/$remote_dir");
	    $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
        $rc = $ftp->setcwd("$bass1_ftp_basedir/$remote_dir");
        $ftpmsg = $ftp->error;
    }

    if (!$rc)
    {
	 	showTime();print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
	 	return 0;
	}
	my $rdir;
    if ($protocol eq "FTP") {
        $rdir = $ftp->pwd;
    } elsif ($protocol eq "SFTP") {
        $rdir = $ftp->cwd;
    }
	showTime();print "remote dir is $rdir \n";

	showTime();print "open $des_file to upload file \n";
	#检查文件名和内容是否正确
	my $i;
	my $pass_rc=0;
	foreach $i ( @info)
	{

        my $filename=(split(' ',$i))[0];
        $filename=~s/\s+//;
        my ($fileprefix,$fileext)=split('\.',$filename);
        $fileprefix=~s/\s+//;
        my $tmp_filename=$fileprefix."\.tmp";
		my $pass_ret;
        if ($protocol eq "FTP") {
            $pass_ret = $ftp->put("$des_dir/$filename","$tmp_filename");
            $ftpmsg=$ftp->message;
        } elsif ($protocol eq "SFTP") {
            $pass_ret = $ftp->put("$des_dir/$filename","$tmp_filename");
            $ftpmsg=$ftp->error;
        }
        if(defined($pass_ret))
        {
            showTime();print "File $filename pass successful.\n";
            if ($protocol eq "FTP") {
                $ftp->rename("$tmp_filename","$filename");
            } elsif ($protocol eq "SFTP") {
                $ftp->rename("$tmp_filename","$filename");
            }
        }
        else
        {
            showTime();print "File $filename pass failed:$!\n";
            $pass_rc=$pass_rc+1;
        }
        showTime();print "put $des_dir/$filename msg:$ftpmsg. \n";
	}

	if (  $pass_rc == 0 )
	{
       	my $pass_ret;
        my $ftpmsg;
        if ($protocol eq "FTP") {
            $pass_ret = $ftp->put("$des_dir/$des_file");
            $ftpmsg=$ftp->message;
        } elsif ($protocol eq "SFTP") {
            $pass_ret = $ftp->put("$des_dir/$des_file");
            $ftpmsg=$ftp->error;
        }

	   showTime();print "put $des_dir/$des_file msg: $ftpmsg. \n";
	   if(defined($pass_ret))
   	   {
	    	showTime();print "File $des_file pass successful.\n";
	    	$pass_rc=0;
	   }
	   else
	   {
	    	showTime();print "File $des_file pass failed:$!\n";
	    	$pass_rc=1;
	   }
	}

	return $pass_rc;

}

sub FTP_put_file
{
    my($protocol,$ftp,$des_dir,$des_file,$remote_dir) = @_;

	my $pass_rc=0;

	unless(@_ == 5)
	{
		showTime();print "parameter is not right\n";
		return 1;
	}

	unless($remote_dir =~ m'^/.*')
	{
		$remote_dir = "$remote_dir";
	}
	my $rc;
	my $ftpmsg;
    if ($protocol eq "FTP") {
        $rc = $ftp->cwd("$remote_dir");
        $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
        $rc = $ftp->setcwd("$remote_dir");
        $ftpmsg = $ftp->error;
    }

    if (!$rc )
    {
	 	showTime();print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
	 	return 0;
	}
	my $rdir;
    if ($protocol eq "FTP") {
        $rdir = $ftp->pwd;
    } elsif ($protocol eq "SFTP") {
        $rdir = $ftp->cwd;
    }
    
	showTime();print "remote dir is $rdir \n";

	showTime();print "open $des_file to upload file \n";

	my $pass_ret;
    if ($protocol eq "FTP") {
        $pass_ret = $ftp->put("$des_dir/$des_file");
        $ftpmsg=$ftp->message;
    } elsif ($protocol eq "SFTP") {
        $pass_ret = $ftp->put("$des_dir/$des_file");
        $ftpmsg=$ftp->error;
    }
    
	showTime();print "put $des_dir/$des_file msg: $ftpmsg. \n";
	if(defined($pass_ret))
    {
	 	showTime();print "File $des_file pass successful.\n";
	 	$pass_rc=0;
	}
	return $pass_rc;
}

sub FTP_get_file
{
	my($protocol,$ftp,$des_dir,$des_file,$local_dir) = @_;
	unless(@_ == 5)
	{
		print "parameter is not right\n";
		return "FALSE";
	}

	unless($des_dir =~ m'^/.*')
	{
		$des_dir = "$des_dir";
	}

	my $cwd_rc;
	my $ftpmsg;

    if ($protocol eq "FTP") {
	    $cwd_rc = $ftp->cwd("$bass1_ftp_basedir/$des_dir");
	    $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
        $cwd_rc = $ftp->setcwd("$bass1_ftp_basedir/$des_dir");
        $ftpmsg = $ftp->error;
    }

    if (!$cwd_rc )
    {
	 	showTime();print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
	 	return "FALSE";
	}

    my $get_rc;
    if ($protocol eq "FTP") {
        $get_rc = $ftp->get("$des_file","$local_dir/$des_file");
    } elsif ($protocol eq "SFTP") {
        $get_rc = $ftp->get("$des_file","$local_dir/$des_file")
    }
	

	unless($get_rc)
	{
		print "File $des_file get failed:$!\n";
		return "FALSE";
	}

	return "$local_dir/$des_file";
}

sub FTP_mget_file
{
	my($ftp,$ftpip,$ftpuser,$ftppasswd,$ftpport,$des_dir,$des_file,$local_dir) = @_;
	unless(@_ == 8)
	{
		print "parameter is not right\n";
		return "FALSE";
	}

	unless($des_dir =~ m'^/.*')
	{
		$des_dir = "$des_dir";
	}
	my $cwd_rc = $ftp->cwd("$bass1_ftp_basedir/$des_dir");
	my $ftpmsg = $ftp->message;

  if (!$cwd_rc )
  {
	 	showTime();print "Can't FIND Direcotry $bass1_ftp_basedir/$des_dir:$ftpmsg\n";
	 	$cwd_rc = $ftp->cwd("$des_dir");
	 	$ftpmsg = $ftp->message;
	 	if (!$cwd_rc )
  		{
  		showTime();print "And can't FIND Direcotry $des_dir:$ftpmsg\n";
	 		return "FALSE";
	 	}
	 	else
	 	{
	 		showTime();print "But we are open the Direcotry $des_dir:$ftpmsg\n";
	 	}
	}
	#print "START_FTP_GET:$des_file get to $local_dir/$des_file\n";
	my $ftp_commond="timeout $SECOND_WAIT axel -n6 -a ftp://$ftpuser:$ftppasswd\@$ftpip:$ftpport$des_dir/$des_file -o $local_dir/$des_file";
	$ftp_commond=~s/\$/\\\$/g;
	showTime();print "ftp_commond:$ftp_commond\n";
	`$ftp_commond`;
	
	print "daoleweihao:$?\n";
	if($?)
	{
		showTime();print "File $des_file get failed:$!\n";
		return "FALSE";
	}

	return "$local_dir/$des_file";
}

sub FTP_del_file
{
	my($protocol,$ftp,$des_dir,$des_file) = @_;
	unless(@_ == 4)
	{
		print "parameter is not right\n";
		return 0;
	}

	unless($des_dir =~ m'^/.*')
	{
		$des_dir = "$des_dir";
	}

    my $cwd_rc;
    my $ftpmsg;
    if ($protocol eq "FTP") {
	    $cwd_rc = $ftp->cwd("$bass1_ftp_basedir/$des_dir");
	    $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
        $cwd_rc = $ftp->setcwd("$bass1_ftp_basedir/$des_dir");
        $ftpmsg = $ftp->error;
    }

    if (!$cwd_rc )
    {
	 	showTime();print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
	 	return 0;
	}
	my $del_rc;
    if ($protocol eq "FTP") {
        $del_rc = $ftp->delete("$des_file");
    } elsif ($protocol eq "SFTP") {
        $del_rc = $ftp->remove("$des_file");
    }

	unless($del_rc)
	{
	 	print "File $des_file delete failed:$!\n";
	 	return 0;
	}

	return 1;
}

sub FTP_rename_file
{
	my($protocol,$ftp,$des_dir,$des_file) = @_;
	unless(@_ == 3)
	{
		print "parameter is not right\n";
		return 0;
	}

	unless($des_dir =~ m'^/.*')
	{
		$des_dir = "$des_dir";
	}

    my $cwd_rc;
    my $ftpmsg;
    if ($protocol eq "FTP") {
	    $cwd_rc = $ftp->cwd("$bass1_ftp_basedir/$des_dir");
	    $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
        $cwd_rc = $ftp->setcwd("$bass1_ftp_basedir/$des_dir");
        $ftpmsg = $ftp->error;
    }

    if (!$cwd_rc )
    {
	 	showTime();print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
	 	return 0;
	}
	my $del_rc;
    if ($protocol eq "FTP") {
        $del_rc = $ftp->rename("$des_file","APPDONE$des_file");
    } elsif ($protocol eq "SFTP") {
        $del_rc = $ftp->rename("$des_file","APPDONE$des_file");
    }

    unless($del_rc)
    {
	 	showTime();print "File $des_file delete failed:$!\n";
	 	return 0;
	}

	return 1;
}

sub check_list_file
{
	#分别检查目录是否存在
	my($dir,$filename,$tgt_getdir,$tgtdir) = @_;
	#print "check:$dir,$filename\n";

	unless((-d "$tgt_getdir") && (-e "$tgt_getdir" ) && (-r "$tgt_getdir") && (-w "$tgt_getdir"))
	{
		showTime();print "Can not found the dir:$tgt_getdir!\n";
		unless ( mkpath("$tgt_getdir",0, 0755) ) 
		{
			showTime();print "and Create directory failed!\n";
			return 9;
		}
		#print "The dir have created by EDT!\n";
	}

	unless((-d "$tgtdir") && (-e "$tgtdir" ) && (-r "$tgtdir") && (-w "$tgtdir"))
	{
		showTime();print "Can not found the dir:$tgtdir!\n";
		unless ( mkpath("$tgtdir",0, 0755) )
		{
			showTime();print "and Create directory failed!\n";
			return 9;
		}
		#print "The dir have created by EDT!\n";
	}

	my $fulllistname="$dir/$filename";

	unless((-d "$dir") && (-e "$dir" ) && (-r "$dir") && (-w "$dir"))
	{
		showTime();print "Can not found the dir:$dir!\n";
		unless ( mkpath("$dir",0,0755) )
		{
			showTime();print "and Create directory failed!\n";
			return 9;
		}
		#print "The dir have created by EDT!\n";
	}
	#print "The dir have exists!\n";
	#print "fulllistname:$fulllistname\n";
	if ((-e "$fulllistname" ) && (-r "$fulllistname") && (-w "$fulllistname"))
	{
		#print "The $fulllistname have exists and can read/write!\n";
	}
	else {
		unless(open(CRTLISTFILE,">$fulllistname")){
			showTime();print "The listfile created failed!\n";
			return 8;
		}
		close(CRTLISTFILE);
	}
	return 0;
}

sub select_list_file
{ #检查“$filename:$filesize”，字符串是否存在于记录文件$list_file中，存在则返回1，不存在返回0；
	my($list_file,$filename,$filesize) = @_;
	#print "select_list_file:$list_file:$filename\n";
	unless(open(SELLISTFILE,"$list_file"))
	{
		showTime();print "Can not read the list_file!\n";
		return -1;
	}
	#print "IS open the checkFTPEDLIST\n";
	my @scanlistarr = <SELLISTFILE>;
	close(SELLISTFILE);
	for (my $i=0; $i <= $#scanlistarr; $i++){
		chomp ($scanlistarr[$i]);
		if ("$filename:$filesize" eq "$scanlistarr[$i]")
		{
			#print "select_list_file::$filename eq $scanlistarr[$i]\n";
			return 1;
			last;
		}
	}
	return 0;
}

sub insert_list_file
{
	#将“$filename:$filesize”串写入文件$list_file中备案。
	my($list_file,$filename,$filesize) = @_;

	unless(open(LISTFILE,">>$list_file"))
	{
		showTime();print "Can not write the list_file!\n";
		return 1;
	}
	print LISTFILE ("$filename:$filesize\n");
	close(LISTFILE);
	return 0;
}

sub delete_list_file
{
	my($list_file,$filename,$filesize) = @_;
	chomp($list_file);
	chomp($filename);
	chomp($filesize);
	#print "deleteArgs:$list_file,$filename\n";
	my @resut_list=();
	unless(open(rLISTFILE,"$list_file"))
	{
		showTime();print "Can not write the list_file to delete!\n";
		return -1;
	}
	my @listfile=<rLISTFILE>;
	close(rLISTFILE);

	unless(open(wLISTFILE,">$list_file"))
	{
		showTime();print "Can not write the list_file to delete!\n";
		return -1;
	}
	my $listlen=$#listfile;
	#print "testing11:$listlen,@listfile\n";
	foreach (@listfile)
	{
		chomp($_);
		chomp($filename);
		chomp($filesize); 
		if ($_ ne "$filename:$filesize")
		{
			#push (@resut_list,$_);
			print wLISTFILE ("$_\n");
		}
		else
		{
			#print "BIDUI::$_,$filename!\n";
		}
	}

	#while (my $listdel = (pop @resut_list)){
	#	print wLISTFILE ("$listdel\n");
	#}
	close(wLISTFILE);
	return 0;
}


sub Ftp_download_file
{
	#从远端服务器$ftp，检查$srcdir目录下的文件$filename，是否在本地$check_ftplog_dir目录下的$check_ftp_cfg_file文件中进行过记录，且字节数是否与记录的$filesize匹配，如果没有，则FTP到本地的临时目录$get_tgtdir，再成功移动到$tgtdir目录下；
	my ($protocol,$ftp,$srcipstr,$check_ftplog_dir,$check_ftp_cfg_file,$filename,$srcdir,$get_tgtdir,$tgtdir,$filesize)=@_;
	my ($ftpip,$ftpuser,$ftppasswd,$srcport)=split(/\t/,$srcipstr);
	chomp($filename);
	chomp($filesize);
	my $isListFile=check_list_file("$check_ftplog_dir","$check_ftp_cfg_file","$get_tgtdir","$tgtdir");
	if ($isListFile == 0)
	{
		#print "LISTFILE:$check_ftplog_dir/$check_ftp_cfg_file\n";
		my $isFtp = select_list_file("$check_ftplog_dir/$check_ftp_cfg_file","$srcdir/$filename",$filesize);
		#print "isFtp:$isFtp\n";
		if ($isFtp == 0)
		{
			#print "DOWN_INFO:$ftp,$srcdir,$filename,$get_tgtdir\n";
			my $ftp_rc=FTP_get_file($protocol,$ftp,$srcdir,"$filename","$get_tgtdir");
			#print "\ncheck2\n";
			if($ftp_rc ne "FALSE")
			{  #insert the ftp Record file
				insert_list_file("$check_ftplog_dir/$check_ftp_cfg_file","$srcdir/$filename",$filesize);
			}
			else {
				showTime();print "download file: $filename error!!!!\n";
				return 12;
			}
		}
		else
		{
			#showTime();print "The file $filename have transmited by EDT!\n";
			return 11;
		}
	}
	else {
		showTime();print "download record's file or dir can not created!\n";
		return 10;
	}
	return 0;
}


sub FTP_get_list
{
	my($protocol,$ftp,$des_dir,$fileclass) = @_;
	unless(@_ == 4 or @_ == 3)
	{
		showTime();print "parameter is not right\n";
		return 0;
	}

	if (@_ == 3){
		$fileclass=" ";
	}

	my @filelist;

	my $dir;
    my $cwd_rc;
    my $ftpmsg;
    if ($protocol eq "FTP") {
        $dir = $ftp->pwd;
        $cwd_rc =$ftp->cwd("$bass1_ftp_basedir/$des_dir");
        $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
        $dir = $ftp->cwd;
        $cwd_rc = $ftp->setcwd("$bass1_ftp_basedir/$des_dir");
        $ftpmsg = $ftp->error;
    }
    
    if (!$cwd_rc )
    {
        showTime();#print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
        print "Can't FIND Direcotry $des_dir\n";
        return 0;
    }

	#获取文件列表
	my $flag = 0;
    if ($protocol eq "FTP") {
        $flag = eval{
                @filelist = $ftp->ls("$fileclass");
            };
    } elsif ($protocol eq "SFTP") {
        if ($fileclass eq " ") {
            $flag = eval{
                my $ls = $ftp->ls(".", wanted => sub {
                            my $entry = $_[1];
                            S_ISREG($entry->{a}->perm)
                        });
                for my $file (@$ls) {
                    push(@filelist, $file->{filename});
                }
            };
        } else {
            $flag = eval{
                my $ls = $ftp->ls(".", wanted => qr /^${fileclass}$/);
                for my $file (@$ls) {
                    push(@filelist, $file->{filename});
                }
            };
        }
    }


	unless(!$@)
	{
		showTime();print "List Direcotry Failed:$@\n";
		return 0;
	}

	return \@filelist;
}

sub FTP_get_long_list
{   #从远端服务器$ftp获取相应目录$des_dir中，符合模式$model的文件列表，列表以全路径体现
	my($protocol,$ftp,$des_dir,$model,$fileclass) = @_;
	unless(@_ == 5 or @_ == 4)
	{
		showTime();print "parameter is not right\n";
		return 0;
	}

	if (@_ == 4){
		$fileclass=" ";
	}

	my @srcfilelist;
	my @dealfilelist;

    my $dir;
    my $cwd_rc;
    my $ftpmsg;
    if ($protocol eq "FTP") {
        $dir = $ftp->pwd;
        $cwd_rc =$ftp->cwd("$bass1_ftp_basedir/$des_dir");
        $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
        $dir = $ftp->cwd;
        $cwd_rc = $ftp->setcwd("$bass1_ftp_basedir/$des_dir");
        $ftpmsg = $ftp->error;
    }
	if (!$cwd_rc )
	{
		showTime();print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
		return 0;
	}
	#获取文件列表
	my $flag = 0;
    if ($protocol eq "FTP") {
        $flag = eval{
                @srcfilelist = $ftp->dir("$fileclass");
            };
    } elsif ($protocol eq "SFTP") {
        if ($fileclass eq " ") {
            $flag = eval{
                my $ls = $ftp->ls(".", wanted => sub {
                            my $entry = $_[1];
                            S_ISREG($entry->{a}->perm)
                        });
                for my $file (@$ls) {
                    push(@srcfilelist, $file->{longname});
                }
            };
        } else {
            $flag = eval{
                my $ls = $ftp->ls(".", wanted => qr /^${fileclass}$/);
                for my $file (@$ls) {
                    push(@srcfilelist, $file->{longname});
                }
            };
        }
    }
	if ($#srcfilelist < 0)
	{
		showTime();print "List Direcotry Failed:$@\n";
		return 0;
	}
	chomp(@srcfilelist);
	foreach my $srcfile (@srcfilelist) {
		my @dirfilemap=split(' ',$srcfile);
		if ($dirfilemap[0] eq "total" || $dirfilemap[0] eq "." || $dirfilemap[0] eq ".."){next;}
		my $filename=$dirfilemap[8]; #3
		my $filesize=$dirfilemap[4]; #2
		
		chomp($filename);
		if ($filename =~ m/^$model$/){
			push (@dealfilelist,"$filename")
		}
	}
	return \@dealfilelist;
}

sub FTP_get_info
{   #从远端服务器$ftp，对应目录$des_dir中，找到相应文件$fileclass。
	# 根据$sleepflag标识，0：直接传输；1：间隔1秒取两次实际字节，判断字节数不再1秒内发生变化的进行传输；2：与控制文件中数据文件字节数$CTLfilesize，匹配进行传输；确认传输，返回文件字节数；
	my($protocol,$ftp,$des_dir,$fileclass,$sleepflag,$CTLfilesize) = @_;
	unless(@_ == 5 or @_ == 6)
	{
		showTime();print "parameter is not right\n";
		return -1;
	}
	
	if (!$CTLfilesize){
		$CTLfilesize=0;
	}
	else {
		#print "CTLfilesize:$CTLfilesize\n";
	}
	chomp($fileclass);
	my $testsize1;
	my $testsize2;
	my @srcfilelist1;
	my @srcfilelist2;
	my $filetype;
	my $cwd_rc;
    my $ftpmsg;
	if ($protocol eq "FTP") {
		$ftp->cwd("$des_dir");
	    $ftpmsg = $ftp->message;
    } elsif ($protocol eq "SFTP") {
		$ftp->setcwd("$des_dir");
        $ftpmsg = $ftp->error;
        if ($ftpmsg == 0) {
            $cwd_rc = 1;
        }
	} 
	
	if (!$cwd_rc )
	{
		showTime();print "Can't FIND Direcotry $des_dir:$ftpmsg\n";
		return -1;
	}

	#获取文件信息
	my $flag =0;
	
	if ($protocol eq "FTP") {
		$flag = eval{
				@srcfilelist1 = $ftp->dir("${fileclass}");
			};
	} elsif ($protocol eq "SFTP") {
		$flag = eval{
                my $ls = $ftp->ls(".", wanted => qr /^${fileclass}$/);
                for my $file (@$ls) {
                    push(@srcfilelist1, $file->{longname});
                }
			};
	}
	if ($flag==2 || !@srcfilelist1){
		showTime();print "The File $fileclass is not exists!\n";
		return -2;
	}

	unless(!$@)
	{
		showTime();print "List Direcotry Failed:$@\n";
		return -2;
	}
	$testsize1=(split(' ',$srcfilelist1[0]))[4]; #2
	$filetype=substr((split(' ',$srcfilelist1[0]))[0],1,1);

	#[2].进行与控制文件字节数进行比对校验，效率较高
	if ($sleepflag==2 && @_ == 5){
		if ($testsize1 eq $CTLfilesize){
			return $testsize1;
		}
		else {
			return -1;
		}
	}

	#[0].不校验字节数，直接传输。
	if ($sleepflag==0)
	{
		return $testsize1;
	}

	#[1].进行文件sleep方式校验，因下载需时间等待，会减慢传输效率。
	sleep(1);
	
	if ($protocol eq "FTP") {
		$flag = eval{
				@srcfilelist2 = $ftp->dir("${fileclass}");
			};
	} elsif ($protocol eq "SFTP") {
		$flag = eval{
				my $ls = $ftp->ls(".", wanted => qr /^${fileclass}$/);
                for my $file (@$ls) {
                    push(@srcfilelist2, $file->{longname});
                }
			};
	}
	unless(!$@)
	{
		showTime();print "List Direcotry Failed:$@\n";
		return -2;
	}
	$testsize2=(split(' ',$srcfilelist2[0]))[4]; #2
	if ($testsize1 != $testsize2){
		return -1;
	}
	return $testsize1;
}


1;
__END__
